package com.sqlweb.sqlweb.base;

import lombok.extern.slf4j.Slf4j;
import org.rosuda.JRI.Rengine;

import java.util.Arrays;

@Slf4j
public class CallRService {


    public static void main(String[] args) {


        String[] Rargs = {"--no-save"}; // 配置 R 引擎参数
        Rengine re = new Rengine(Rargs, false, null);

        // 等待 R 引擎启动
        if (!re.waitForR()) {
            log.error("Cannot load R");
            return;
        }

        try {
            // 2. 加载 R 脚本
            String scriptPath = "E:/CAT/fun_ayuan_test_finish.R";
            re.eval("source('" + scriptPath + "')");

            // 3. 定义输入参数
            double currentTheta = -0.433;  // 这里定义 currentTheta
            int[] respData = {2, 1, 2, 2, 2, 2, 2, 2, 2};
            String itemParPath = "E:/CAT/Item parameter file.csv"; // 确保路径正确

            int maxNI = 9;
            double maxSE = -0.316;

            // 4. 将参数传递到 R
            re.assign("currentTheta", new double[]{currentTheta}); // 传递为数值向量
            re.assign("respData", respData); // 传递为数值向量
            re.assign("itemParPath", itemParPath); // 传递文件路径
            re.assign("maxNI", new double[]{maxNI}); // 传递最大项目数
            re.assign("maxSE", new double[]{maxSE}); // 传递最大标准误差

            log.info("Parameter currentTheta: " + currentTheta);
            log.info("Parameter respData: " + Arrays.toString(respData));
            log.info("Parameter itemParPath: " + itemParPath);

            // 5. 执行 R 命令
            String rCommand = "try(selectQuestion(currentTheta, respData, itemParPath, maxNI, maxSE), silent=TRUE)";
            log.info("Executing R command: " + rCommand);
            Object result = re.eval(rCommand);

            // 6. 检查 R 返回结果
            if (result instanceof org.rosuda.REngine.REXP) {
                org.rosuda.REngine.REXP rResult = (org.rosuda.REngine.REXP) result;

                if (rResult.inherits("try-error")) {
                    log.error("R Execute Exception: " + rResult.asString());
                } else if (rResult.isString()) {
                    String[] returnedStrings = rResult.asStrings();
                    log.info("R returned strings: " + Arrays.toString(returnedStrings));
                } else if (rResult.isNumeric()) {
                    double[] returnedNumbers = rResult.asDoubles();
                    log.info("R returned numeric values: " + Arrays.toString(returnedNumbers));
                } else if (rResult.isList()) {
                    org.rosuda.REngine.RList returnedList = rResult.asList();
                    log.info("R returned a list with size: " + returnedList.size());
                } else {
                    log.error("Unexpected return type from R.");
                }
            } else {
                log.error("Unexpected return type from R.");
            }

        } catch (Exception e) {
            log.error("An error occurred while communicating with R.", e);
        } finally {
            // 7. 关闭 R 引擎
            re.end();
        }
    }
}
